﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Utils;
using System.Threading;
using HostAssistantCore;
using HostAssistantBLL;

namespace HostAssistant
{
    public partial class dblist : U1UserControl
    {
        public dblist()
        {
            InitializeComponent();
        
        }

        #region sitelist_Load

        private void sitelist_Load(object sender, EventArgs e)
        {

            TextBoxToolV2.SetWatermark(txtdbname, "数据库名");
            var dblist= dbbll.GetsiteList(null, null);
            gridControl1.DataSource = dblist;

            lblmsg.Text = "当前数据库数量:"+ dblist.Count();


        }

        #endregion

        #region 查询

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            select();
            splashScreenManager1.CloseWaitForm();
            
        }


        void select()
        {
           
            string t = null;
            if (comboBoxEdit1.Text != "数据库类型")
            {
                t = comboBoxEdit1.Text;
            }
            var dblist  = dbbll.GetsiteList(txtdbname.Text, t);
            gridControl1.DataSource = dblist;

            lblmsg.Text = "当前数据库数量:" + dblist.Count();

        }

        #endregion

        #region 创建mysql


        private void btnadd_Click(object sender, EventArgs e)
        {
          
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mysql");
            if (mysql == null)
            {
              
                
                DevExpress.XtraEditors.XtraMessageBox.Show("首次使用请先配置数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }




            adddb f2 = new adddb();
            f2.ShowDialog(this);
        } 
        #endregion

        #region 列表双击
        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            int selectedHandle;
            selectedHandle = this.gridView1.GetSelectedRows()[0];
             string  id=  this.gridView1.GetRowCellValue(selectedHandle, "ID").ToString();
            editdb f2 = new editdb(id);
            f2.ShowDialog(this);
        }
        #endregion

        #region 创建sql

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mssql");
            if (mysql == null)
            {


                DevExpress.XtraEditors.XtraMessageBox.Show("首次使用请先配置数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            addsqldb f2 = new addsqldb();
            f2.ShowDialog(this);
        }
        #endregion

        #region 删除
        /// <summary>
        /// /*删除*/
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

            if (gridView1.FocusedRowHandle < 0) return;
            int selectedHandle;
            selectedHandle = this.gridView1.GetSelectedRows()[0];
            string id = this.gridView1.GetRowCellValue(selectedHandle, "ID").ToString();
          
           if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }
           else
            {
                try
                {

             
                splashScreenManager1.ShowWaitForm();
                splashScreenManager1.SetWaitFormCaption("请等待");
                splashScreenManager1.SetWaitFormDescription("加载中...");


                  dbbll.delete(id);
           
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);



                    select();
                    //string t = null;
                    //if (comboBoxEdit1.Text != "数据库类型")
                    //{
                    //    t = comboBoxEdit1.Text;
                    //}





                    //gridControl1.DataSource = dbbll.GetsiteList(txtdbname.Text, t);









                }
                catch(Exception ex )
                {
                    NLogger.Error("删除数据库："+ ex.ToString());
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
              
            }


        }
        #endregion

        #region 刷新列表
        /// <summary>
        /// 刷新列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            
            try
            {

                OpHelper.CallWithTimeout(Addmssql, 10000);

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                select();
            }
          


        }
        void Addmssql()
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            dbbll.Reshmydblist();
            dbbll.Reshmsdblist();
            splashScreenManager1.CloseWaitForm();
        }



        #endregion
    }
}
